<style lang="less">
    @import '../../styles/loading.less';
</style>

<template>
    <div>
        <Col span="12">
            <Card>
                <p slot="title" style="height:25px;">
                    <Icon type="plus-round"></Icon> {{webcam_id?'编辑':'新增'}}轮播图
                </p>
                <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
                    <Form-item label="推流地址" prop="push_url">
                        <Input v-model="formValidate.push_url" placeholder="Enter..."></Input>
                    </Form-item>
                    <Form-item label="直播地址" prop="live_url">
                        <Input v-model="formValidate.live_url" placeholder="Enter..."></Input>
                    </Form-item>
                    <Form-item label="省市区" prop="area">
                        <Col span="24"><al-selector v-model="area" data-type="name" level="2"/></Col>
                    </Form-item>
                    <Form-item label="详细地址" prop="full_address">
                        <Input v-model="formValidate.full_address" placeholder="Enter..."></Input>
                    </Form-item>

                    <Form-item label="备注" prop="remarks">
                        <Input type="textarea" v-model="formValidate.remarks" placeholder="Enter..."></Input>
                    </Form-item>

                    <Form-item>
                        <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                        <Button type="ghost" v-if="!webcam_id" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
                    </Form-item>
                </Form>
            </Card>
        </Col>
    </div>
</template>

<script>
    import Vue from 'vue';
    import iviewArea from 'iview-area';
    Vue.use(iviewArea);
    import uploadImg from '../website/upload';
    export default {
        components:{
            'uploadImg':uploadImg,
        },
        data () {
            return {
                webcam_id: '',
                area: ['浙江省', '杭州市', '滨江区'],
                formValidate: {
                    push_url: '',
                    live_url: '',
                    remarks: '',
                    full_address: '',
                },
                ruleValidate: {
                    push_url: [
                        { required: true, message: '请填写推流地址' },
                    ],
                    live_url: [
                        { required: true, message: '请填写直播地址' },
                    ],
                    full_address: [
                        { required: true, message: '请填写地址' },
                    ],
                },
            }
        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let url = this.webcam_id ? 'admin/webcam/edit' : 'admin/webcam/add';
                        let postData = this.formValidate;
                        postData.webcam_id = this.webcam_id;
                        postData.district = this.area[2];
                        this.$http.post(url, this.$qs.stringify(postData)).then((res) => {
                            var d = res.data;
                            if (d.code == 0) {
                                this.$Notice.success({
                                    title: 'Success',
                                    desc: '操作成功'
                                });
                                this.$router.push({
                                    name: 'webcamList'
                                });
                            } else {
                                this.$Notice.error({
                                    title: 'Error Warning',
                                    desc: `Error：${d.msg}<br>`
                                });
                            }
                        });
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            uploadImg (data) {
                console.log(data);
                this.formValidate.cover_img = data;
            },
            getOne () {
                this.$http.post('admin/webcam/getOne', this.$qs.stringify({webcam_id: this.webcam_id})).then((res) => {
                    var d = res.data;
                    if (d.code == 0) {
                        this.formValidate = {
                            push_url: d.data.push_url,
                            live_url: d.data.live_url,
                            remarks: d.data.remarks,
                            full_address: d.data.full_address,
                        };
                        this.area = [d.data.province, d.data.city, d.data.district];
                    } else {
                        this.$Notice.error({
                            title: 'Error Warning',
                            desc: `Error：${d.msg}<br>`
                        });
                    }
                });
            },
        },
        mounted() {
            this.$nextTick(() => {
                this.webcam_id = this.$route.query.webcam_id;
                if (this.webcam_id) {
                    this.getOne();
                }
            });
        },
    };
</script>
